package com.ruoyi.transaction.service.impl;

import com.google.common.collect.Lists;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.transaction.domain.OrderItemTmp;
import com.ruoyi.transaction.mapper.OrderItemTmpMapper;
import com.ruoyi.transaction.service.IOrderItemTmpService;
import com.ruoyi.transaction.vo.OrderItemTmpVo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by gzdmt01 on 2019/3/29.
 */
@Service
public class OrderItemTmpService implements IOrderItemTmpService {
    @Autowired
    private OrderItemTmpMapper orderItemTmpMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Override
    public AjaxResult insert(OrderItemTmp item) {

            int count=orderItemTmpMapper.insert(item);
            if(count>0)
                return AjaxResult.success();
            else
                return AjaxResult.error("未知错误");
            }

    @Override
    public AjaxResult batchInsert(List<OrderItemTmp> items, Integer userId) {
        items = setUserId(items,userId);

        int count=orderItemTmpMapper.batchInsert(items);
        if(count>0)
            return AjaxResult.success();
        else
            return AjaxResult.error("未知错误");
    }

    private List<OrderItemTmp> setUserId(List<OrderItemTmp> items, Integer userId) {
        for(OrderItemTmp temp : items)
            temp.setUserId(userId);
        return items;
    }


    @Override
    public List<OrderItemTmpVo> listOrederItemTmp(Long parentUserId, OrderItemTmp orderItemTmp) {
      List<Long> chilrenUserIdList=  getChildrenUserId(parentUserId);
        List<OrderItemTmp> orderItemTmplist=orderItemTmpMapper.selectByUserIds(chilrenUserIdList,orderItemTmp);
        List<OrderItemTmpVo> orderItemTmpVolist=Lists.newArrayList();
        for(OrderItemTmp temp:orderItemTmplist){
            orderItemTmpVolist.add(OrderItemTmpVo.TranFrom(temp));
        }
        return orderItemTmpVolist;
    }

    @Override
    public OrderItemTmp selectById(Integer orderItemTmpId) {
        return orderItemTmpMapper.selectByPrimaryKey(orderItemTmpId);
    }

    @Override
    public int editSaveOrderItemTmp(OrderItemTmp orderItemTmp) {
        return orderItemTmpMapper.updateByPrimaryKeySelective(orderItemTmp);
    }

    @Override
    public AjaxResult deleteByIds(String itemIds, Long userId) {
        if(StringUtils.isBlank(itemIds) || userId == null)
            return AjaxResult.error("非法操作");
        Integer[] ids= com.ruoyi.common.utils.StringUtils.StringToInt(itemIds.split(","));
        List<Integer> itemIdList = Lists.asList(ids[0], ids);
        int count=orderItemTmpMapper.deleteByPrimaryKeys(ids,userId);
        return count>0?AjaxResult.success():AjaxResult.error();
    }

    @Override
    public List<OrderItemTmp> listOrederItemTmpByIds(Long userId, Integer[] ids) {
        return orderItemTmpMapper.selectByUserAndIds(userId,ids);
    }

    private List<Long>  getChildrenUserId(Long userId){
        List<Long> chilrenUserIdList= Lists.newArrayList(userId);
        List<Long> currentChilrenUserIdList=sysUserMapper.selectUserIdsByParentUserId(userId);
        return chilrenUserIdList;
    }
    private void getChildrenUserId(List<Long> chilrenUserIdList,List<Long> currentChilrenUserIdList ){
        if(CollectionUtils.isEmpty(currentChilrenUserIdList))
            return;
        for(Long currentUserId :currentChilrenUserIdList){
            chilrenUserIdList.add(currentUserId);
            List<Long> nextChildrenUserIdList=sysUserMapper.selectUserIdsByParentUserId(currentUserId);
            getChildrenUserId(chilrenUserIdList,currentChilrenUserIdList);
        }

    }
    private boolean checkItemIsExist(OrderItemTmp item ) {
        if(item==null|| StringUtils.isBlank(item.getExpressNo())||item.getExpressCompanyId()==null){
            return false;
        }
        int count=orderItemTmpMapper.countByExpressCompanyIdAndExpressNo(item.getExpressCompanyId(),item.getExpressNo());
        return count==0;
    }


}
